package com.fk.loki.circle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.fk.loki.R;


/**
 * 圆形头像
 * @author Hilex
 * @date 2021-3-4
 */
public class CircleView extends View {

    private Paint mPaint;
    private Rect mRect;
    private int mCircleRes;

    private final static int DEFAULT_HEIGHT=100;
    private final static int DEFAULT_WIDTH=100;

    public CircleView(Context context){
        this(context,null);
    }

    public CircleView(Context context, AttributeSet attrs){
        this(context,attrs,0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyle){
        super(context,attrs,defStyle);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.CircleView,defStyle,0);
        mCircleRes=typedArray.getResourceId(R.styleable.CircleView_circleRes,R.drawable.aobing);
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height=DEFAULT_HEIGHT;
        int width=DEFAULT_WIDTH;

        int HEIGHT_MODE=MeasureSpec.getMode(heightMeasureSpec);
        int HEIGHT_SIZE=MeasureSpec.getSize(heightMeasureSpec);
        int WIDTH_MODE=MeasureSpec.getMode(widthMeasureSpec);
        int WIDTH_SIZE=MeasureSpec.getSize(widthMeasureSpec);

        if(HEIGHT_MODE==MeasureSpec.EXACTLY){
            height=HEIGHT_SIZE;
        }else if(HEIGHT_MODE==MeasureSpec.AT_MOST){
            height=Math.min(HEIGHT_SIZE,height);
        }
        if(WIDTH_MODE==MeasureSpec.EXACTLY){
            width=WIDTH_SIZE;
        }else if(WIDTH_MODE==MeasureSpec.AT_MOST){
            width=Math.min(width,WIDTH_SIZE);
        }
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRect=clipRect(new Rect(0,0,w,h));
        mPaint=new Paint();
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),mCircleRes);
        BitmapShader bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scale=(float)mRect.width()/(float)bitmap.getWidth();
        Matrix matrix=new Matrix();
        matrix.setScale(scale,scale);
        bitmapShader.setLocalMatrix(matrix);
        mPaint.setShader(bitmapShader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mRect!=null){
            canvas.drawCircle(mRect.centerX(),mRect.centerY(),mRect.width()/2f,mPaint);
        }
    }

    /**
     * 保持中心不变，剪成正方形Rect
     * @param rect 原来的Rect
     * @return 正方形的Rect
     */
    private Rect clipRect(Rect rect){
        int cx=rect.centerX();
        int cy=rect.centerY();
        int side=(Math.min(rect.width(),rect.height()))/2;
        return new Rect(cx-side,cy-side,cx+side,cy+side);
    }

    public void setCircleRes(int res){
        mCircleRes=res;
    }

}
